# coding=utf-8

import D2Ext

from .CButton import CButton
from .CImage import CImage
from .CLabel import CLabel
from .Constants import *


class CDialog(CImage):

    def __init__(self, parent=None):
        super(CDialog, self).__init__(parent)
        self.title = ""
        self.title_x = 0
        self.title_y = 0
        self.title_color = D2Color.GREEN
        self.text = ""
        self.text_x = 0
        self.text_y = 0
        self.text_color = D2Color.WHITE
        self.ok_button = CButton(self)
        self.ok_button.set_image("data\\SJ_PATH\\InputDlg")
        self.ok_button.set_button_frame(3, 4)
        self.ok_button.connect(CButton.SIGNAL_ONCLICK, self.on_ok)

        self.cancel_button = CButton(self)
        self.cancel_button.set_image("data\\SJ_PATH\\InputDlg")
        self.cancel_button.set_button_frame(1, 2)
        self.cancel_button.connect(CButton.SIGNAL_ONCLICK, self.on_cancel)

    def set_title(self, text, color=D2Color.GREEN):
        self.title = text
        self.title_color = color

    def set_text(self, text, color=D2Color.WHITE):
        self.text = text
        self.text_color = color

    def set_ok_text(self, text):
        self.ok_button.set_text(text)

    def set_cancel_text(self, text):
        self.cancel_button.set_text(text)

    def set_position(self, x, y):
        # super(CDialog, self).set_position(x, y)
        self.dx = (D2Ext.View_GetWindowWidth() - self.width) / 2
        self.dy = (D2Ext.View_GetWindowHeight() - self.height) / 2
        self.ok_button.set_position(self.dx + 32, self.dy + self.height - 20 - self.ok_button.height)
        self.cancel_button.set_position(self.dx + 138, self.dy + self.height - 20 - self.cancel_button.height)

        t_width = D2Ext.View_GetPixelLen(self.title)
        self.title_x = self.dx + (self.width - t_width) / 2 - 12
        self.title_y = self.dy + 24

        self.text_x = self.dx + (self.width - t_width) / 2 - 12
        self.text_y = self.dy + 70

    def on_ok(self):
        D2Ext.View_PrintTextOnTop(u'on ok！', 1)
        self.hide()

    def on_cancel(self):
        D2Ext.View_PrintTextOnTop(u'on cancel！', 1)
        self.hide()

    def draw(self):
        if self.image_file:
            D2Ext.Image_SetFrame(self.image_file, self.frame)
            D2Ext.Image_Draw(self.image_file, self.dx, self.dy + self.height, self.opacity)

        D2Ext.View_PrintString(self.title, self.title_x, self.title_y, self.title_color, self.opacity)
        D2Ext.View_PrintString(self.text, self.text_x, self.text_y, self.text_color, self.opacity)
        D2Ext.View_PrintString(u"确定", self.ok_button.x + 2, self.ok_button.y + self.ok_button.height + 14, self.color, self.opacity)
        D2Ext.View_PrintString(u"取消", self.cancel_button.x + 2, self.ok_button.y + self.cancel_button.height + 14, self.color, self.opacity)

    def show(self):
        super(CDialog, self).show()
        self.ok_button.show()
        self.cancel_button.show()

    def hide(self):
        super(CDialog, self).hide()
        self.ok_button.hide()
        self.cancel_button.hide()
